/* $RCSFile: HtmlStringDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/HtmlStringDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;

using Path2     =   AddonNice.Settings.Path;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// Summary description for HtmlStringDataType.
	/// </summary>
	public class HtmlStringDataType  : StringDataType
	{
		protected int					controlHeight	= 300;
		protected PortalModuleControl	Module			= null;

		public HtmlStringDataType(PortalModuleControl	mod) : base()
		{
			Module			=	mod;
			InnerDataType	=	PropertiesDataType.String;
            FullWidth       =   true;
		}

		public HtmlStringDataType(PortalModuleControl	mod,string val) : base (val)
		{
			Module			=	mod;
			InnerDataType	=	PropertiesDataType.String;
            FullWidth       =   true;
		}

		public override string Description
		{
			get
			{
				return "HtmlString";
				}
			}

		protected override void InitializeComponents()
		{
			if (HttpContext.Current == null)
			{
                const string msg    =   "[E]HtmlStringDataType InitializeComponents no Context.";
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
			    throw new Exception(msg);
			}
			PlaceHolder PlaceHolderHTMLEditor	=	new PlaceHolder();
			IHtmlEditor locEditor		        =	null;
			
            PortalSettings pS				    =	(PortalSettings) HttpContext.Current.Items[StStr.strPortalSettings];
			HtmlEditorDataType	h			    =	new HtmlEditorDataType();
			locEditor				            =	h.GetEditor(PlaceHolderHTMLEditor,Module,ReadOnly);

            locEditor.Width				        =	controlWidth;
			locEditor.Height				    =	new System.Web.UI.WebControls.Unit(controlHeight);
			innerControl						=	PlaceHolderHTMLEditor;
            _IsInitialized                      =   true;
		}

		
        public override void DataBind()
        {
			try
			{
				//Update value in control
				((IHtmlEditor)innerControl.Controls[0]).TextContent = Value2Save;
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlStringDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
        }

        public override Control EditControl
		{
			get
			{
				if ( innerControl == null )
					InitializeComponents();
				return innerControl;
			}
			set
			{
				if( value is PlaceHolder)
				{
					innerControl	=	value;
					//Update value from control
					innerValue = ((IHtmlEditor)innerControl.Controls[0]).TextContent;
                    return;
				}
                string msg  =   string.Format("[E]HtmlStringDataType EditControl IHtmlEditor values is required, a '{0}' is given.",value);
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(msg, "EditControl");
			}
		}
	}
}	

